//
//  RW_UserSingleListView.m
//  SleepApp
//
//  Created by chuangqi on 2021/4/7.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_UserSingleListView.h"
#import "RW_UserSingleListItemCell.h"
#import "RW_PushPlayersTools.h"
#import "RW_PlaySingleCollectionReusableView.h"

@interface RW_UserSingleListView (){
    BOOL _isEdit;
}
@property (nonatomic,strong) RW_PlaySingleCollectionReusableView *wlclass_header_view;

@property (nonatomic,strong) WLCollectionView *wlclass_collection_view;
@end
@implementation RW_UserSingleListView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        [self addSubview:self.wlclass_header_view];
        [self addSubview:self.wlclass_collection_view];
        [self.wlclass_header_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self).mas_offset( -kBottomSafeHeight);
            make.height.mas_offset(45);
        }];
        [self.wlclass_collection_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(self);
            make.bottom.mas_equalTo(self).mas_offset(-45 - kBottomSafeHeight);
        }];
    }
    return self;
}

-(void)setWlclass_view_model:(RW_UserSingleListViewModel *)wlclass_view_model{
    _wlclass_view_model = wlclass_view_model;
    [self reloadCollectionList];
}
-(void)reloadCollectionList{
    RW_PlaySingleCollectionReusableViewModel *headerModel = [[RW_PlaySingleCollectionReusableViewModel alloc]init];
    headerModel.userSingleModel = _wlclass_view_model.wlclass_user_songle_model;
    self.wlclass_header_view.model = headerModel;
    
    [_wlclass_view_model getViewSuccess:^(NSArray<WLBaseCollectionViewLayerSection *> * _Nonnull response) {
        [self reloadSection:response];
    } fail:^(NSError * _Nonnull error) {
        [SVProgressHUD showError_wl:error.domain];
    }];
}

-(void)reloadSection:(NSArray<WLBaseCollectionViewLayerSection *> *)response{
    for (WLBaseCollectionViewLayerSection *section in response) {
        for (WLBaseCollectionViewLayerRow *row in section.item) {
            if ([row.data isKindOfClass:RW_UserSingleListItemCellModel.class]){
                [(RW_UserSingleListItemCellModel *)row.data setIsEdit:_isEdit];
            }
        }
    }
    [self.wlclass_collection_view setAllSection:response];
}

#pragma mark -- 功能
/** 修改当前编辑状态 */
-(void)songEditStatus:(BOOL)isEdit{
    _isEdit = isEdit;
    [self reloadSection:[self.wlclass_collection_view getAllSection]];
}

#pragma mark -- WLCollectionView 代理
- (void)wl_collectionView:(UICollectionView *)collectionView
               layerModel:(WLBaseCollectionViewLayerModel *)layerModel
             layerRowData:(id)rowData
             didIndexPath:(NSIndexPath *)indexPath{
    if ([rowData isKindOfClass:RW_UserSingleListItemCellModel.class]){
        RW_UserSingleListItemCellModel *model = rowData;
        [RW_PushPlayersTools pushSongId:model.user_song_model.xid fromVc:[self viewController_wl]];
    }
}

/** 刷新列表数据 */
-(void)reloadList{
    [self reloadCollectionList];
}

#pragma mark -- 懒加载
-(WLCollectionView *)wlclass_collection_view{
    if (!_wlclass_collection_view){
        _wlclass_collection_view = [WLCollectionView initWithVerticalDelegate:self];
        _wlclass_collection_view.backgroundColor = HexColor(@"f6f6f6");
    }
    return _wlclass_collection_view;
}

-(RW_PlaySingleCollectionReusableView *)wlclass_header_view{
    if (!_wlclass_header_view){
        _wlclass_header_view = [[RW_PlaySingleCollectionReusableView alloc]init];
    }
    return _wlclass_header_view;
}
@end
